//
//  HXDHTTPSessionManager.m
//  qingDaoCircleOfLife
//
//  Created by huxuedong on 15/11/23.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

#import "HXDHTTPSessionManager.h"
#import "HXDHomeDataModel.h"

@implementation HXDHTTPSessionManager

//实例化单例对象
+ (instancetype)shareInstance {
    static HXDHTTPSessionManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[HXDHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:kBaseURL]];
        //设置解析器类型
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    });
    return manager;
}

- (void)getURL:(NSString *)url withParameter:(NSDictionary *)parameter andWithSuccessBlock:(loadDataSuccessed)successed andWithFailedBlock:(loadDataFailed)failed {
    //通过AFN发送GET请求
    [self GET:url parameters:parameter success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        successed(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failed(error);
    }];
}

/*
- (void)postURL:(NSString *)url withParameter:(NSDictionary *)parameter andWithSuccessBlock:(loadDataSuccessed)successed andWithFailedBlock:(loadDataFailed)failed {
}
*/

+ (void)getURL:(NSString *)url withParameter:(NSDictionary *)parameter andWithSuccessBlock:(loadDataSuccessed)successed andWithFailedBlock:(loadDataFailed)failed {
    [[self shareInstance] getURL:url withParameter:parameter andWithSuccessBlock:successed andWithFailedBlock:failed];
}

/*
+ (void)postURL:(NSString *)url withParameter:(NSDictionary *)parameter andWithSuccessBlock:(loadDataSuccessed)success andWithFailedBlock:(loadDataFailed)failed {
    [[self shareInstance] postURL:url withParameter:parameter andWithSuccessBlock:successed andWithFailedBlock:failed];
}
*/

- (void)getURL:(NSString *)url withParameter:(NSDictionary *)parameter andWithClassName:(NSString *)className andWithSuccessBlock:(loadDataSuccessed)successed andWithFailedBlock:(loadDataFailed)failed {
    [self GET:url parameters:parameter success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"home.plist" ofType:nil];
        NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
        Class classer = NSClassFromString(className);
        successed([[classer alloc] initWithDictionary:dict error:nil]);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failed(error);
    }];
}

+ (void)getURL:(NSString *)url withParameter:(NSDictionary *)parameter andWithClassName:(NSString *)className andWithSuccessBlock:(loadDataSuccessed)successed andWithFailedBlock:(loadDataFailed)failed {
    return [[self shareInstance] getURL:url withParameter:parameter andWithClassName:className andWithSuccessBlock:successed andWithFailedBlock:failed];
}

- (void)getHomeDataWithParameter:(NSDictionary *)parameter andWithSuccessBlock:(loadDataSuccessed)successed andWithFailedBlock:(loadDataFailed)failed {
    [self getURL:@"?anu=api/1/index/get_index_info" withParameter:parameter andWithSuccessBlock:^(id json) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"home.plist" ofType:nil];
        NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
        //字典转模型
        HXDHomeDataModel *dataModel = [[HXDHomeDataModel alloc] initWithDictionary:dict error:nil];
        successed(dataModel);
    } andWithFailedBlock:^(NSError *error) {
        failed(error);
    }];
}

+ (void)getHomeDataWithParameter:(NSDictionary *)parameter andWithSuccessBlock:(loadDataSuccessed)successed andWithFailedBlock:(loadDataFailed)failed {
    [[self shareInstance] getHomeDataWithParameter:parameter andWithSuccessBlock:successed andWithFailedBlock:failed];
}

@end
